/**  
* Copyright (c) 2009, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// created by noellynch
// Nov 2, 2009

package com.sceneworker.wizards;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

import net.miginfocom.swing.MigLayout;

import com.jme.image.Texture;
import com.jme.scene.TriMesh;
import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.geom.TangentBinormalGenerator;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.editors.glslshadereditor.ShaderEditorPanel;

public class ConvertToNormalMapped extends WizardBase {

	private		JLabel			mcl_baseMap;
	private		JLabel			mcl_normalMap;
	private		JLabel			mcl_specMap;
	
	public	ConvertToNormalMapped(JDialog par) {
		super(par);
	}
	
	@Override
	public void doWizardUI(Object cl_treeParent) {
		final	WizardBase		lcl_this = this;
		
		setLayout(new MigLayout("inset 5"));
		
		JButton		lcl_locateBaseMap = SwingComponentFactory.makeAButton("Locate Texture");
		add(lcl_locateBaseMap);
		mcl_baseMap = SwingComponentFactory.makeALabel("");
		add(mcl_baseMap, "wrap");
		lcl_locateBaseMap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File		lcl_f = GenericUtils.getAnImageFile(null);
				if(lcl_f == null) { 
					return; 
				}
				
				mcl_baseMap.setText(lcl_f.getPath());
				lcl_this.containerDialog.pack();
				lcl_this.containerDialog.setLocationRelativeTo(null);
			}
		});
		
		JButton		lcl_locateNMap = SwingComponentFactory.makeAButton("Locate Normal Map");
		add(lcl_locateNMap);
		mcl_normalMap = SwingComponentFactory.makeALabel("");
		add(mcl_normalMap, "wrap");
		lcl_locateNMap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File		lcl_f = GenericUtils.getAnImageFile(null);
				if(lcl_f == null) { 
					return; 
				}
				
				mcl_normalMap.setText(lcl_f.getPath());
				lcl_this.containerDialog.pack();
				lcl_this.containerDialog.setLocationRelativeTo(null);
			}
		});
		
		JButton		lcl_locateSpecMap = SwingComponentFactory.makeAButton("Locate Specular Map");
		add(lcl_locateSpecMap);
		mcl_specMap = SwingComponentFactory.makeALabel("");
		add(mcl_specMap, "wrap");
		lcl_locateSpecMap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File		lcl_f = GenericUtils.getAnImageFile(null);
				if(lcl_f == null) { 
					return; 
				}
				
				mcl_specMap.setText(lcl_f.getPath());
				lcl_this.containerDialog.pack();
				lcl_this.containerDialog.setLocationRelativeTo(null);
			}
		});
	}

	@Override
	public void executeWizard(Object cl_treeParent) {
		if(!(cl_treeParent instanceof TriMesh)) {
			SimpleAlert.doAlert("You can only execute this on a TriMesh!", this);
			return;
		}
		
		TriMesh				lcl_mesh = (TriMesh)cl_treeParent;
		TextureState		lcl_ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
		
		Texture				lcl_baseT = TextureManager.loadTexture(mcl_baseMap.getText(), Texture.MinificationFilter.Trilinear, 
				Texture.MagnificationFilter.Bilinear);
		lcl_ts.setTexture(lcl_baseT, 0);
		
		Texture				lcl_NT = TextureManager.loadTexture(mcl_normalMap.getText(), Texture.MinificationFilter.Trilinear, 
				Texture.MagnificationFilter.Bilinear);
		lcl_ts.setTexture(lcl_NT, 1);
		
		Texture				lcl_specT = TextureManager.loadTexture(mcl_specMap.getText(), Texture.MinificationFilter.Trilinear, 
				Texture.MagnificationFilter.Bilinear);
		lcl_ts.setTexture(lcl_specT, 2);
		
		lcl_mesh.copyTextureCoordinates(0, 1, 1);
		lcl_mesh.copyTextureCoordinates(0, 2, 1);
		lcl_mesh.setRenderState(lcl_ts);
		
		GLSLShaderObjectsState		lcl_shader = DisplaySystem.getDisplaySystem().getRenderer().createGLSLShaderObjectsState();
		lcl_mesh.setRenderState(lcl_shader);
		
		TangentBinormalGenerator.generate(lcl_mesh);
		URL		lcl_vert = ShaderEditorPanel.class.getClassLoader().getResource(SceneWorkerGlobals.DATA_DIR + "shaders/normalMapSpecularWithAttrs/normalMapSpecAttrs.vert");
		URL		lcl_frag = ShaderEditorPanel.class.getClassLoader().getResource(SceneWorkerGlobals.DATA_DIR + "shaders/normalMapSpecularWithAttrs/normalMapSpecAttrs.frag");
		lcl_shader.load(lcl_vert, lcl_frag);
		
		lcl_shader.setUniform("baseMap", 0);
		lcl_shader.setUniform("normalMap", 1);
		lcl_shader.setUniform("specularMap", 2);
		
		lcl_shader.setAttributePointer("modelTangent", 3, true, 0, lcl_mesh.getTangentBuffer());
    	lcl_shader.setAttributePointer("modelBinormal", 3, true, 0, lcl_mesh.getBinormalBuffer());
    	
    	lcl_mesh.updateRenderState();
	}

}
